package com.ibeeking.found.report.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibeeking.found.report.rest.config.prop.JimuConfigProp;
import com.ibeeking.found.report.rest.service.IReportManageService;
import com.ibeeking.found.report.service.common.constant.enums.DeleteStatusEnum;
import com.ibeeking.found.report.service.common.constant.enums.JimuReportTypeEnum;
import com.ibeeking.found.report.service.common.constant.enums.PublishStatusEnum;
import com.ibeeking.found.report.service.common.constant.enums.ReportTypeEnum;
import com.ibeeking.found.report.service.common.dos.JimuReportDO;
import com.ibeeking.found.report.service.common.dos.ReportDO;
import com.ibeeking.found.report.service.common.query.ReportManagePageQuery;
import com.ibeeking.found.report.service.common.vo.SimpleJimuReportVO;
import com.ibeeking.found.report.service.common.vo.SimpleReportVO;
import com.ibeeking.found.report.service.mapper.JimuReportMapper;
import com.ibeeking.found.report.service.mapper.ReportMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.date.DateUtil;
import org.jeecg.modules.jmreport.desreport.entity.JimuReport;
import org.jeecg.modules.jmreport.desreport.service.IJimuReportService;
import org.jeecg.modules.jmreport.desreport.service.IJmReportDbService;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportManageServiceImpl implements IReportManageService {

    private static final DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Resource
    private ReportMapper dmpReportMapper;

    @Resource
    private JimuReportMapper jmReportMapper;

    @Resource
    private IJimuReportService jimuReportServiceImpl;

    @Resource
    private JimuConfigProp jimuConfigProp;

    @Override
    public Page<SimpleReportVO> dmpPageList(ReportManagePageQuery pageQuery) {
        LambdaQueryWrapper<ReportDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(pageQuery.getReportType()), ReportDO::getReportType, pageQuery.getReportType())
                .eq(pageQuery.getPublishStatus() !=null, ReportDO::getPublishStatus, pageQuery.getPublishStatus())
                .like(StringUtils.isNotEmpty(pageQuery.getReportName()), ReportDO::getReportName, pageQuery.getReportName())
                .orderByDesc(ReportDO::getCreateTime);
        Page page = new Page(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<ReportDO> entityDOPages = dmpReportMapper.selectPage(page, queryWrapper);
        if(entityDOPages!=null && entityDOPages.getSize() > 0) {
            Page<SimpleReportVO> entityVOPage = BeanUtil.convertPage(entityDOPages, SimpleReportVO.class);
            List<ReportDO> entityDOList = entityDOPages.getRecords();
            List<SimpleReportVO> resultVOList = entityDOList.stream().map(s -> {
                SimpleReportVO entityVO = BeanUtil.convertBean(s, SimpleReportVO.class);
                entityVO.setReportTypeDesc(ReportTypeEnum.getDescByCode(s.getReportType()));
                if(s.getCreateTime() != null){
                    entityVO.setCreateTime(dateTimeformatter.format(s.getCreateTime()));
                }
                return entityVO;
            }).collect(Collectors.toList());
            entityVOPage.setRecords(resultVOList);
            return entityVOPage;
        }
        return new Page<>();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeDmpStatus(Long id, Integer status) {
        ReportDO entityDO = dmpReportMapper.selectById(id);
        entityDO.setPublishStatus(status);
        return dmpReportMapper.updateById(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delDmpReport(Long id) {
        ReportDO entityDO = dmpReportMapper.selectById(id);
        if (entityDO == null) {
            return true;
        }
        //删除会员偏好关系表数据
        return dmpReportMapper.deleteById(id) > 0;
    }

    @Override
    public Page<SimpleJimuReportVO> jmPageList(ReportManagePageQuery pageQuery) {
        LambdaQueryWrapper<JimuReportDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(pageQuery.getReportType()), JimuReportDO::getType, pageQuery.getReportType())
                .eq(pageQuery.getPublishStatus() != null, JimuReportDO::getStatus, pageQuery.getPublishStatus())
                .like(StringUtils.isNotEmpty(pageQuery.getReportName()), JimuReportDO::getName, pageQuery.getReportName())
                .eq(JimuReportDO::getDelFlag, DeleteStatusEnum.UNDELETED.getCodeValue())
                .orderByDesc(JimuReportDO::getCreateTime);
        Page page = new Page(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<JimuReportDO> entityDOPages = jmReportMapper.selectPage(page, queryWrapper);
        if(entityDOPages!=null && entityDOPages.getSize() > 0) {
            Page<SimpleJimuReportVO> entityVOPage = BeanUtil.convertPage(entityDOPages, SimpleJimuReportVO.class);
            List<JimuReportDO> entityDOList = entityDOPages.getRecords();
            List<SimpleJimuReportVO> resultVOList = entityDOList.stream().map(s -> {
                SimpleJimuReportVO entityVO = BeanUtil.convertBean(s, SimpleJimuReportVO.class);
                if(StringUtils.isEmpty(s.getStatus())){
                    entityVO.setStatus(PublishStatusEnum.DISABLE.getCodeValue());
                }else{
                    entityVO.setStatus(Integer.valueOf(s.getStatus()));
                }
                if(StringUtils.isNotEmpty(s.getType())){
                    entityVO.setTypeDesc(JimuReportTypeEnum.getDescByType(s.getType()));
                }
                if(s.getCreateTime() != null){
                    entityVO.setCreateTime(DateUtil.formatToStr(s.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                }
                return entityVO;
            }).collect(Collectors.toList());
            entityVOPage.setRecords(resultVOList);
            return entityVOPage;
        }
        return new Page<>();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeJmStatus(String id, Integer status) {
        JimuReport jimuReport = new JimuReport();
        jimuReport.setId(id);
        jimuReport.setStatus(status.toString());
        return jimuReportServiceImpl.updateById(jimuReport) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delJmReport(String id) {
        JimuReport jimuReport = new JimuReport();
        jimuReport.setId(id);
        jimuReport.setDelFlag(DeleteStatusEnum.DELETED.getCodeValue());
        return jimuReportServiceImpl.updateById(jimuReport) > 0;
    }

    @Override
    public String getJimuReportServerDomain(){
        return jimuConfigProp.getAddress().concat(":").concat(jimuConfigProp.getPort()).concat(jimuConfigProp.getContextPath());
    }
}
